package cn.broadmax.boot.modules.sys.controller;


import cn.broadmax.boot.base.SuperController;
import cn.broadmax.boot.log.SysLog;
import cn.broadmax.boot.modules.sys.bean.SysUserBean;
import cn.broadmax.boot.modules.sys.bean.SysUserSearch;
import cn.broadmax.boot.modules.sys.entity.SysRole;
import cn.broadmax.boot.modules.sys.entity.SysUser;
import cn.broadmax.boot.base.message.SResult;
import cn.broadmax.boot.modules.sys.bean.user.UserCreateRequest;
import cn.broadmax.boot.modules.sys.service.ISysRoleService;
import cn.broadmax.boot.modules.sys.service.ISysUserService;
import cn.broadmax.boot.util.SAssert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 管理平台用户 前端控制器
 * </p>
 *
 * @author jexx
 * @since 2020-07-21
 */
@Api(tags = "用户")
@RestController
@RequestMapping("/sys/user")
@RequiredArgsConstructor
public class SysUserController extends SuperController {

    private final ISysUserService sysUserService;
    private final ISysRoleService sysRoleService;

    @ApiOperation("新增用户")
    @SysLog(value = "新增用户")
    @PostMapping(value = "")
    public SResult<SysUser> addUser(@RequestBody UserCreateRequest request){
        SysUser user = sysUserService.addUser(request);
        return SResult.ok("", user);
    }

    @ApiOperation("根据主键删除用户")
    @SysLog(value = "删除用户")
    @DeleteMapping(value = "/{userId}")
    public SResult<Boolean> deleteUserById(@PathVariable String userId){
        boolean success = sysUserService.removeById(userId);
        return SResult.ok("", success);
    }

    @ApiOperation("根据主键更新用户信息,以及角色关系")
    @SysLog(value = "更新用户")
    @PutMapping(value = "/{userId}")
    public SResult<SysUser> updateUserById(@PathVariable String userId, @RequestBody SysUserBean bean){
        SAssert.notNull(bean, "bean must be not null");
        SysUser user = sysUserService.updateUserById(userId, bean);
        return SResult.ok("", user);
    }

    @ApiOperation("分页查询用户")
    @GetMapping(value = "")
    public SResult<IPage<SysUser>> pageUsers(SysUserSearch search){
        LambdaQueryWrapper<SysUser> wrapper = SysUserSearch.createWrap(search);
        IPage<SysUser> page = sysUserService.page(getPage(), wrapper);
        return SResult.ok("", page);
    }

    @ApiOperation("根据ID获取用户")
    @GetMapping(value = "/{userId}")
    public SResult<SysUserBean> pageUsers(@PathVariable String userId){
        SysUser user = sysUserService.getById(userId);
        if(user == null){
            return null;
        }
        SysUserBean bean = new SysUserBean();
        BeanUtils.copyProperties(user, bean);

        List<SysRole> roles =  sysRoleService.getRolesByUserId(userId);
        bean.setRoleIds(roles.stream().map(SysRole::getId).collect(Collectors.toList()));
        return SResult.ok("", bean);
    }

}
